var user;
var SEND_WAIT = 5;//发送间隔5s


$(document).ready(function() {
	//confirm
	init();
	user = sessionStorage.getItem("user");
	debug(user);
	if (user) {
		user = JSON.parse(user);
		initUserCard();
	} else {
		outputError("尚未登录！", "/login.html");
	}
});

function init(){
	$('#sendFrame').slideToggle();
}

function outputError(msg, forward){
	showFcDialog("错误警告", msg, "确定", function(){
		if (forward) {
			window.location.href = forward;
		}
	});
}

function initUserCard() {
	var face = '<img src="images/ali/ali' + user.face + '_m.jpg" width="50" height="50">';
	var score = getUserLv(user.score);
	var state = "灯塔";
	$("#cmf").html(face);
	$("#myc_name").html(user.name);
	$("#myc_state").html(state);
	$("#myc_score").html(score);
}

/**
 * 可能需要抽象到一个方法中
 * @param sc
 * @returns {String}
 */
function getUserLv(sc){
	if (sc < 50) {
		return "磨墨画童&nbsp;&nbsp;" + sc;
	} else if ( sc > 49 && sc < 200) {
		return "初级学徒&nbsp;&nbsp;" + sc;
	} else if ( sc > 199 && sc < 500) {
		return "中级学徒&nbsp;&nbsp;" + sc;
	} else if ( sc > 499 && sc < 1000) {
		return "高级学徒&nbsp;&nbsp;" + sc;
	} else if ( sc > 499 && sc < 1000) {
		return "高级学徒&nbsp;&nbsp;" + sc;
	} else if ( sc > 999 && sc < 1800) {
		return "初窥门径&nbsp;&nbsp;" + sc;
	} else if ( sc > 1799) {//其他的以后慢慢写
		return "返璞归真&nbsp;&nbsp;" + sc;
	}
}

function throwone(){
	$('#sendFrame').slideToggle();
}

function validMsg(title, content){
	if (title && content) {
		if (title.length > 1 && content.length > 1) {
			return true;
		}
	}
	
	return false;
}

function send(){
	var title = $("#msgTitle").val();
	var content = $("#msgContent").val();
	var valid = validMsg(title, content);
	if (!valid) {
		showFcDialog("错误的文字", "标题和内容至少2个字", "确定", function(){});
		return;
	}
	var uid = user._id;
//	debug(title);
//	debug(content, "#debug1");
	$.ajax({
		cache : false,
		type : "GET",
		dataType : "json",
		url : "/send",
		data : {
			"uid" : uid,
			"title" : title,
			"content" : content
		},
		error : function() {
			outputError("无法连接到服务器哦，亲");
		},
		success : function(data) {
			var type = data.type;
			if (type == "ok") {
				showFcDialog("发送成功", "发送成功", "确定", function(){});
				waitNext(SEND_WAIT);
				$("#msgTitle").val("");
				$("#msgContent").val("");
			} else if (type == "error") {
				outputError("发送失败！");
			}
		}
	});
}

function waitNext(wait){
	if (wait >= 0) {
		$("#sendBtn").val(wait + " 秒后可投递");
		$("#sendBtn").attr("disabled",true);
		$("#sendBtn").attr("className","btn_grey");
		wait --;
		setTimeout(function(){
			waitNext(wait);
		}, 1000);
	} else {
		$("#sendBtn").removeAttr("disabled");
		$("#sendBtn").val("扔出去");
		$("#sendBtn").attr("className","btn_blue");
	}
}

function salvage(){
	var uid = user._id;
	$.ajax({
		cache : false,
		type : "GET",
		dataType : "json",
		url : "/salvage",
		data : {
			"uid" : uid
		},
		error : function() {
			outputError("无法连接到服务器哦，亲");
		},
		success : function(data) {
			var type = data.type;
			if (type == "ok") {
				if (data.msg != null) {
					showFcDialog("成功啦", "成功捞到1个瓶子", "确定", function(){});
				}
			} else if (type == "error") {
				outputError("发送失败！");
			}
		}
	});
}

